import Vue from 'vue';
import VueRouter from 'vue-router';
import { getRoleMenuListAPI } from '../api/backStage';
import { Router_tree } from '../utils/router_tree';
import stores from '../store/index';
Vue.use(VueRouter);
// 大家都能看到的
const routes = [
    {
        path: '/',
        redirect: '/frontDesk'
    },
    {
        path: '/main',
        name: 'home',
        component: () => import('@/components/common/Home.vue')
    },
    {
        path: '/frontDesk',
        name: 'frontDesk',
        component: () => import(/* webpackChunkName: "frontDesk" */ '@/views/frontDesk/frontHome/index.vue'),
        meta: { title: '前台页面' }
    },
    {
        path: '/frontDesk/privacy',
        name: 'frontDesk/privacy',
        component: () => import(/* webpackChunkName: "frontDesk" */ '@/views/frontDesk/privacy/Privacy.vue'),
        meta: { title: '隐私声明' }
    },
    {
        path: '/frontDesk/reg',
        component: () => import(/* webpackChunkName: "frontDesk" */ '@/views/frontDesk/Login/login.vue'),
        meta: { title: '注册' }
    },
    {
        path: '/404',
        component: () => import(/* webpackChunkName: "404" */ '@/views/404'),
        meta: { title: '404' }
    }
    // {
    //     path: '*',
    //     redirect: '/404'
    // }
];
const router = new VueRouter({
    mode: 'hash',
    routes
});

router.beforeEach(async (to, from, next) => {
    document.title = `${to.meta.title} | 甘美医院药物试验平台`;
    if (to.path == '/main' || to.path == '/main/dashboard') {
        if (!stores.state.router.routeList.length) {
            let token = localStorage.getItem('gcp_token');
            let oprid = localStorage.getItem('gcp_oprid');
            await getRoleMenuListAPI({
                token: token,
                OPRID: oprid
            })
                .then(res => {
                    if (res.code == 'succ') {
                        let routelist = JSON.parse(JSON.stringify(res.result));
                        let routeCblList = JSON.parse(JSON.stringify(res.result));
                        let yijiList = Router_tree(routelist, {
                            id: 'sysCode',
                            pid: 'parentCode'
                        });
                        let mains = routes.filter(item => item.path == '/main')[0];
                        mains.children = yijiList;
                        mains.redirect = '/main/dashboard';
                        // 侧边栏
                        let sidebarLsit = yijiList.filter(item => item.sysCode != '008');
                        // let sidebarLsits=[]
                        // if(routelist.length){
                        // let sidebarLsits= Router_tree(routeCblList,{
                        //   id:'sysCode',
                        //   pid:'parentCode'
                        // })
                        // }
                        // stores.dispatch('Roterlist',sidebarLsits );

                        stores.dispatch('router/Roterlist', sidebarLsit);
                        // 添加404页面
                        let err = { path: '/:pathMatch(.*)*', redirect: '/404' };
                        router.addRoute(mains);
                        router.addRoute(err);
                        // stores.dispatch('AppState',true );
                        next();
                        // let url = localStorage.getItem('url');
                        // let urls = url?url:'/main/dashboard'
                        router.push('/main/dashboard');
                    } else {
                        next();
                    }
                })
                .catch(err => {
                    next();
                });
        } else {
            if (to.path == '/main') {
                next();
            } else {
                next();
            }
        }
    } else {
        next();
    }
});

//解决编程式路由往同一地址跳转时会报错的情况
const originalPush = VueRouter.prototype.push;
const originalReplace = VueRouter.prototype.replace;
//push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject);
    return originalPush.call(this, location).catch(err => err);
};
//replace
VueRouter.prototype.replace = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject);
    return originalReplace.call(this, location).catch(err => err);
};
export default router;
// // 解决路由无权限跳转报错
// const originalPush = Router.prototype.push;
// Router.prototype.push = function push(location, onResolve, onReject) {
//     if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject);
//     return originalPush.call(this, location).catch(err => err);
// };
